const path = require("path");
const fs = require("fs");
const commandOpt = process.env.npm_config_argv;
let site = 'default';
let port = 8888;
if (commandOpt) {
  const argv = JSON.parse(commandOpt);
  const arr = argv.original;
  let type = arr[1];
  if (type == 'serve' || type == 'build') {
    site = arr[2] && arr[2].replace("--", "") || 'default';
    port = arr[3] && arr[3].replace("--", "") || 8888;
    console.log('start:--'+type+'--'+site);
  }
}
const resolve = dir => {
  return path.join(__dirname, dir);
};

const proxyObj =require('./proxy.config.js');

module.exports = {

  baseUrl: "/",
  
  assetsDir: "static",
  
  outputDir: "./dist/" + site + "/",
  
  chainWebpack: config => {

    config.resolve.alias
      .set("@", resolve("src"))
      .set("_c", resolve("src/components"))
      .end();
      
    config
    .entry('app')
    .clear()
    .add("./src/site/" + site + "/main.js")
    .end()
    
    config.externals({
    	"vue": "Vue",
    	"iview": "iview",
    	"vue-router": "VueRouter",
    	"vuex": "Vuex",
    	"axios": "axios",
    	"echarts": "echarts",
    	"lodash":"_",
    	"vue-i18n":"VueI18n"
    }).end()
    
    config
      .plugin("html")
      .tap(args => {
        args[0].template = "./src/site/"+site+"/index.html",
        args[0].favicon = "./src/site/"+site+"/favicon.ico"
        return args;
      }).end()
    
     config.module.rule('images')
      .use('image-webpack-loader')
      .loader('image-webpack-loader')
      .options({
        //disable: true, // webpack@2.x and newer  下面和该属性二选一，通用属性
        mozjpeg: {
          progressive: true,
          quality: 80
        },
        optipng: {
          enabled: false
        },
        pngquant: {
          quality: "80-90",
          speed: 8
        },
        gifsicle: {
          interlaced: false
        },
        /* Compress JPG & PNG images into WEBP
        webp: {
          quality: 80
        }
        */
      })
      .end()      
  },
  
  configureWebpack: {},
  
  configureWebpack: config => {},
  
  transpileDependencies: ['iview/src/*'],

  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "less",
      patterns: [path.resolve(__dirname, "./src/styles/global.less")]
    },
    "uglifyjs-webpack-plugin": {
      uglifyOptions: {
        compress: {
          warnings: false,
          drop_console: true,
          collapse_vars: true,
          reduce_vars: true
        },
        output: {
          beautify: false,
          comments: false
        }
      }
    }
  },

  css: {
    loaderOptions: {
      css: {
        localIdentName: "[name]-[hash]",
        camelCase: "only"
      }
    }
  },

  devServer: {
    port: port,
    proxy: {
      '/xd/xd/api/': proxyObj['xd'],
    }
  }
};


